import {
    colorUISdk
} from "../../config/mp-sdk";
import {updateCurrentPageShare} from "../../mp-sdk/lib/share";

Page({
    data: {
        latitude: '',
        longitude: '',
        send_msg: '',
        created_at: '',
        phone: '',
        from_user_id: '',
        markers: [{
            id: 1,
            latitude: '',
            longitude: '',
            width: '50rpx',
            height: '60rpx',
            alpha: 1,
            callout: {
                content: '对方位置(点击唤起地图导航)',
                color: 'rgb(103,174,241)',
                fontSize: 12,
                borderRadius: 10,
                bgColor: '#fff',
                padding: 6,
                display: 'ALWAYS',
                textAlign: 'center',
                borderWidth: 1,
                borderColor: '#ccc'
            }
        }]
    },
    onLoad(options) {
        const message_id = options.message_id;
        this.getWxMap(message_id);

    },
    openLocation() {
        wx.openLocation({
            latitude: Number(this.data.latitude),
            longitude: Number(this.data.longitude),
            scale: 18,
            success: res => {
            },
            fail: err => {
                console.log(err)
            }
        });
    },
    getWxMap(message_id) {
        //获取微信地图位置
        colorUISdk.request({
            url: 'api/user/get_wx_map',
            method: 'GET',
            data: {
                message_id: message_id
            }
        }).then(res => {
            if (res && res.code === 1) {
                this.setData({
                    latitude: res.data.lat,
                    longitude: res.data.lng,
                    send_msg: res.data.send_msg,
                    markers: [{
                        id: 1,
                        latitude: res.data.lat,
                        longitude: res.data.lng,
                        width: '50rpx',
                        height: '60rpx',
                        alpha: 1,
                        callout: {
                            content: '对方位置(点击唤起地图导航)',
                            color: 'rgb(103,174,241)',
                            fontSize: 12,
                            borderRadius: 10,
                            bgColor: '#fff',
                            padding: 6,
                            display: 'ALWAYS',
                            textAlign: 'center',
                            borderWidth: 1,
                            borderColor: '#ccc'
                        }
                    }],
                    created_at: res.data.created_at,
                    phone: res.data.phone,
                    from_user_id: res.data.from_user_id
                })
            } else {
                wx.showToast({title: res.msg || '获取失败', icon: 'none'});
            }
        }).catch(err => {
            wx.showToast({title: '系统异常', icon: 'none'});
        });

    },
    changeWxNotice(e) {
        if (e.detail) {
            this.subscribe();
        } else {
            colorUISdk.request({
                url: 'api/user/change_wx_notice',
                method: 'POST',
                data: {
                    wx_notice: 0
                }
            }).then(res => {
                if (res.code === 1) {
                    this.setUserInfo(res.data);
                    // 登录成功后立即更新分享配置
                    updateCurrentPageShare();
                    wx.showToast({title: res.msg, icon: 'success'});
                } else {
                    wx.showToast({title: res.msg, icon: 'none'});
                }
            });
        }
    },
    subscribe() {
        wx.requestSubscribeMessage({
            tmplIds: ['nGA2V4J7bVeuKS7xiu4RsAsw91BEB45N13sJ_VNpQmQ'],
            success: res => {
                if (res['nGA2V4J7bVeuKS7xiu4RsAsw91BEB45N13sJ_VNpQmQ'] === 'accept') {
                    colorUISdk.request({
                        url: 'api/user/change_wx_notice',
                        method: 'POST',
                        data: {wx_notice: 1}
                    }).then(res => {
                        if (res.code === 1) {
                            this.setUserInfo(res.data);

                            this.setData({
                                modal: {
                                    show: true,
                                    title: '订阅成功',
                                    content: res.msg
                                }
                            });
                        } else {
                            wx.showToast({title: res.msg, icon: 'none'});
                        }
                    });
                }
            }
        });
    },
    makePhoneCall() {
        wx.makePhoneCall({
            phoneNumber: this.data.phone
        });
    }
})